package thread;

/**
 * Created by qq65827 on 2015/2/3.
 */

// a demo show the different between wait() and the notify()
public class Demo {

    public static void main(String[] args) {
        ThreadDemo b = new ThreadDemo();
        b.start();
        System.out.println("thread b is running...");
        synchronized (b) {
            try {
                System.out.println("waiting for b is complete... ");
                b.wait();
                System.out.println("Completed.Now back to main thread");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Total is :"+b.total);
    }


}

 class ThreadDemo extends Thread {
    int total;
    public void run() {
        try {
            Thread.sleep(3000);
            synchronized (this) {
                System.out.println("Thread B is running...");
                for (int i = 0; i < 100; i++) {
                    total += i;
                    if (i == 30) {
                        Thread.sleep(3000);
                        System.out.println("total is " + total);
                    }
                    this.notify();

                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
